﻿using System;

namespace PARCS.Core
{
    /// <summary>
    /// Contains constants of the system
    /// </summary>
    public static class Const
    {
        #region Public

        public static int HostTCPPort
        {
            get { return _hostTCPPort; }
        }

        public static int HostUDPPort
        {
            get { return _hostUDPPort; }
        }

        public static int DaemonTCPPort
        {
            get { return _daemonTCPPort; }
        }

        public static int DaemonUDPPort
        {
            get { return _daemonUDPPort; }
        }

        public static short PointConnectionPort
        {
            get { return _pointConnectionPort; }
        }

        public static byte HsCreatePoint
        {
            get { return _hsCreatePoint; }
        }

        public static byte HsRemovePoint
        {
            get { return _hsRemovePoint; }
        }

        public static byte HsBeginTask
        {
            get { return _hsBeginTask; }
        }

        public static byte HsEndTask
        {
            get { return _hsEndTask; }
        }

        public static byte HsSetFunc
        {
            get { return _hsSetFunc; }
        }

        public static int HsWait
        {
            get { return _hsWait; }
        }

        public static byte DmExecuteClass
        {
            get { return _dmExecuteClass; }
        }

        public static byte DmLoadClasses
        {
            get { return _dmLoadClasses; }
        }

        public static byte DmLoadDll
        {
            get { return _dmLoadDll; }
        }

        public static byte DmEcho
        {
            get { return _dmEcho; }
        }

        public static byte DmPerformance
        {
            get { return _dmPerformance; }
        }

        public static byte DmReceiveTask
        {
            get { return _dmReceiveTask; }
        }

        public static byte DmConnectPoint
        {
            get { return _dmConnectPoint; }
        }

        public static byte DmConnectWait
        {
            get { return _dmConnectWait; }
        }

        public static byte DmAddPointData
        {
            get { return _dmAddPointData; }
        }

        public static byte DmUDPPing
        {
            get { return _dmUDPPing; }
        }

        public static bool NoTimeouts
        {
            get { return _noTimeouts; }
        }

        #endregion

        #region Private

        private static short _hostTCPPort = 3333;
        private static short _hostUDPPort = 3333;
        private static short _daemonTCPPort = 2222;
        private static short _daemonUDPPort = 2222;
        private static short _pointConnectionPort = 1111;

        private static byte _hsCreatePoint = 1;
        private static byte _hsRemovePoint = 2;
        private static byte _hsBeginTask = 11;
        private static byte _hsEndTask = 12;
        private static byte _hsSetFunc = 13;

        private static int _hsWait = int.MaxValue;

        private static byte _dmExecuteClass = 1;
        private static byte _dmLoadClasses = 2;
        private static byte _dmLoadDll = 3;
        private static byte _dmEcho = 7;
        private static byte _dmPerformance = 8;
        private static byte _dmReceiveTask = 10;
        private static byte _dmConnectPoint = 11;
        private static byte _dmConnectWait = 12;
        private static byte _dmAddPointData = 13;

        private static byte _dmUDPPing = 7;

        private static bool _noTimeouts;

        #endregion
        
        /// <summary>
        /// Parse arguments passed from the console
        /// </summary>
        /// <param name="args">Aruments from the console</param>
        /// <returns>Last element, if he isn't recognized. Otherwise - null</returns>
        public static string ParseArgs(string[] args)
        {
            for (var i = 1; i < args.Length; i++)
            {
                var s = args[i];
                try
                {
                    if (s == "--notimeouts") _noTimeouts = true;
                    else if (s == "--dmport")
                    {
                        if (++i >= args.Length) break;
                        _daemonTCPPort = short.Parse(args[i]);
                    }
                    else if (s == "--dmudpport")
                    {
                        if (++i >= args.Length) break;
                        _daemonUDPPort = short.Parse(args[i]);
                    }
                    else if (s == "--hsport")
                    {
                        if (++i >= args.Length) break;
                        _hostTCPPort = short.Parse(args[i]);
                    }
                    else if (s == "--hsudpport")
                    {
                        if (++i >= args.Length) break;
                        _hostUDPPort = short.Parse(args[i]);
                    }
                    else if (i == args.Length - 1 && s[0] != '-') return s;
                }
                catch (FormatException) { i--; }
            }
            return null;
        }
    }
}
